package com.xs.service.impl;

import com.xs.dao.LogDao;
import com.xs.pojo.Log;
import com.xs.service.LogSercice;
import com.xs.util.DateUtil;
import com.xs.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.omg.CORBA.MARSHAL;

import java.util.Date;
import java.util.List;

public class LogSerciceImpl implements LogSercice {

    /**
     * 查询所有
     */
    //创建sqlSessionFactory 工厂对象
    SqlSessionFactory factory= SqlSessionFactoryUtil.getSqlSessionFactory();
    @Override
    public List<Log> SelectAll() {
        //获取sqlSession对象
        SqlSession sqlSession = factory.openSession();
        //调用Mapper
        LogDao mapper = sqlSession.getMapper(LogDao.class);
        //调用方法
//        Log log=new Log();
//        log.setHead_time(DateUtil.formatDate(new Date()));
        List<Log> logs = mapper.SelectAll();
        //释放资源
        sqlSession.close();
        return logs;
    }

    /**
     * 添加
     * @param log
     * @return
     */
    @Override
    public void Add(Log log) {
        //获取sqlSession对象
        SqlSession sqlSession = factory.openSession();
        //调用Mapper
        LogDao mapper = sqlSession.getMapper(LogDao.class);
        mapper.add(log);
        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }


    /**
     * 根据Id查询
     * @param id
     * @return
     */
    @Override
    public Log SelectById(int id) {

        SqlSession sqlSession = factory.openSession();
        //调用Mapper
        LogDao mapper = sqlSession.getMapper(LogDao.class);
        //调用方法
        Log log = mapper.SelectById(id);

        sqlSession.close();

        return log;
    }

    /**
     * 修改
     * @param log
     */
    @Override
    public void Update(Log log) {
        SqlSession sqlSession = factory.openSession();
        //调用Mapper
        LogDao mapper = sqlSession.getMapper(LogDao.class);

        mapper.Updata(log);

        sqlSession.commit();

        sqlSession.close();
    }

    /**
     * 删除功能
     * @param id
     */
    @Override
    public void Delete(Integer id) {
        SqlSession sqlSession = factory.openSession();
        //调用Mapper
        LogDao mapper = sqlSession.getMapper(LogDao.class);
        mapper.Delete(id);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public List<Log> selectCondition(Log log) {
        SqlSession sqlSession = factory.openSession();
        LogDao mapper = sqlSession.getMapper(LogDao.class);

        // 处理Log条件，模糊表达式
        String user_name = log.getUser_name();
        if (user_name != null && user_name.length() > 0) {
            log.setUser_name("%" + user_name + "%");
        }


        String phon = log.getPhon();
        if (phon!= null && user_name.length() > 0) {
            log.setPhon("%" + phon + "%");
        }


        System.out.println("模糊处理后的值为"+log);


        //调用条件查询方法
        List<Log> logs = mapper.selectCondition(log);
        System.out.println("555555555");
        sqlSession.close();

        return logs;
    }
}
